home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.09 Sep 91 / Bug Parser Code / Mini.file.c next >
Encoding:
C/C++ Source or Header  |  1989-12-09  |  4.7 KB  |  252 lines  |  [TEXT/KAHL]

  1. /*********************************************************************
  2.  
  3.     mini.file.c
  4.     
  5.     file functions for Miniedit
  6.     
  7. *********************************************************************/
  8.     
  9. #include <MacTypes.h>
  10. #include <FileMgr.h>
  11. #include <TextEdit.h>
  12. #include <WindowMgr.h>
  13. #include <StdFilePkg.h>
  14.  
  15. #include "MiniEdit.h"
  16.  
  17. Str255         theFileName;
  18. static int    theVRefNum;
  19.  
  20. extern TEHandle  TEH;
  21. extern WindowPtr myWindow;
  22. extern char         dirty;
  23.  
  24. SetUpFiles()
  25. {
  26.     pStrCopy("\p", theFileName);
  27.     theVRefNum = 0;
  28. }
  29.  
  30. int DoFile( item )
  31. int        item;
  32.  
  33. {
  34.     int     vRef, refNum, io;
  35.     Str255    fn;
  36.  
  37.     switch (item) {
  38.     case fmNew: 
  39.         SetWTitle( myWindow, "\pUntitled");
  40.         ShowWindow( myWindow );
  41.         dirty = 0;
  42.         break;
  43.     case fmOpen:
  44.         if (OldFile(fn, &vRef ))
  45.             if (FSOpen(fn, vRef, &refNum)==noErr) {
  46.                 if (ReadFile( refNum, TEH )) {
  47.                     pStrCopy(fn, theFileName);
  48.                     theVRefNum = vRef;
  49.                     SetWTitle(myWindow, theFileName);
  50.                     dirty = 0;
  51.                 }
  52.                 if (FSClose(refNum)==noErr) ;
  53.                 ShowWindow( myWindow );
  54.                 TESetSelect(0, 0, TEH);
  55.                 ShowSelect();
  56.             }
  57.             else FileError( "\pError opening ", fn );
  58.         break;
  59.     case fmClose:
  60.         if (dirty) {
  61.             ParamText("\pSave changes for “",
  62.                     (theFileName[0]==0) ? "\pUntitled" : (char *)theFileName,
  63.                     "\p”?", "\p");
  64.             switch (Alert(AdviseAlert, 0L)) {
  65.             case aaSave:
  66.                 if (theFileName[0]==0) {
  67.                     fn[0] = 0;
  68.                     if (!SaveAs(fn, &vRef)) return(0);
  69.                 }
  70.                  else if (!SaveFile( theFileName, theVRefNum )) return(0);
  71.                  break;
  72.              case aaCancel: return(0);
  73.              case aaDiscard: dirty = 0;
  74.              }
  75.          }
  76.         CloseMyWindow();
  77.         break;
  78.     case fmSave:
  79.         if (theFileName[0]==0) goto saveas;
  80.         SaveFile(theFileName, theVRefNum);
  81.         break;
  82.     case fmSaveAs:
  83. saveas:
  84.         fn[0] = 0;
  85.         if (SaveAs(fn, &vRef )) {
  86.             pStrCopy(fn, theFileName);
  87.             theVRefNum = vRef;
  88.             SetWTitle(myWindow, theFileName);
  89.         }
  90.         break;
  91.     case fmRevert:
  92.         ParamText("\pRevert to last saved version of “",
  93.                 theFileName, "\p”?", "\p");
  94.         switch (Alert(AdviseAlert, 0L)) {
  95.         case aaSave:
  96.             HidePen();
  97.             TESetSelect( 0, (**TEH).teLength, TEH );
  98.             ShowPen();
  99.             TEDelete(TEH);
  100.             if ((theFileName[0]!=0) &&
  101.                 (FSOpen(theFileName, theVRefNum, &refNum)==noErr)) {
  102.                 dirty = !ReadFile( refNum, TEH ); 
  103.                 /* I don't check for bad read! */
  104.                 if (FSClose(refNum)==noErr) ;
  105.             }
  106.             ShowWindow( myWindow );
  107.             UpdateWindow( myWindow );
  108.          case aaCancel:
  109.          case aaDiscard: return(0);;
  110.          }
  111.  
  112.         break;
  113.     case fmPageSetUp:
  114.         DoPageSetUp();
  115.         break;
  116.     case fmPrint:
  117.         PrintText( (**TEH).hText, (long)(**TEH).teLength, (GrafPtr)myWindow,
  118.                         StringWidth("\pmmmm"), 0L);
  119.         break;
  120.     case fmQuit: 
  121.         if (DoFile(fmClose)) ExitToShell();
  122.     }
  123.     return(1);
  124. }
  125.  
  126. static Point SFGwhere = { 90, 82 };
  127. static Point SFPwhere = { 106, 104 };
  128. static SFReply reply;
  129.  
  130. SaveAs( fn, vRef )
  131. Str255     fn;
  132. int        *vRef;
  133. {
  134.     int refNum;
  135.     
  136.     if (NewFile(fn, vRef)) 
  137.         if (CreateFile(fn, vRef, &refNum)) {
  138.             WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength);
  139.             FSClose( refNum );
  140.             dirty = 0;
  141.             return(1);
  142.         }
  143.         else {
  144.             FileError("\pError creating file ", fn);
  145.         }
  146.     return(0);
  147. }
  148.  
  149. SaveFile( fn, vrn )
  150. Str255    fn;
  151. int        vrn;
  152. {
  153.     int refNum;
  154.     
  155.     if (FSOpen( fn, vrn, &refNum )==noErr) {
  156.         WriteFile(refNum, (*(**TEH).hText), (long)(**TEH).teLength);
  157.         dirty = 0;
  158.         FSClose( refNum );
  159.         return(1);
  160.     }
  161.     else FileError("\pError opening file ", fn);
  162.     return(0);
  163. }
  164.  
  165. NewFile( fn, vRef )
  166. Str255    fn;
  167. int        *vRef;
  168. {
  169.     SFPutFile(SFPwhere, "\p", fn, 0L, &reply);
  170.     if (reply.good) {
  171.         pStrCopy(reply.fName, fn);
  172.         *vRef = reply.vRefNum;
  173.         return(1);
  174.     }
  175.     else return(0);
  176. }
  177.  
  178. OldFile( fn, vRef )
  179. Str255    fn;
  180. int        *vRef;
  181. {
  182.     SFTypeList    myTypes;
  183.     
  184.     myTypes[0]='TEXT';
  185.     SFGetFile( SFGwhere, "\p", 0L, 1, myTypes, 0L, &reply );
  186.     if (reply.good) {
  187.         pStrCopy( reply.fName, fn );
  188.         *vRef = reply.vRefNum;
  189.         return(1);
  190.     }
  191.     else return(0);
  192. }
  193.  
  194. CreateFile( fn, vRef, theRef )
  195. Str255    fn;
  196. int        *vRef;
  197. int        *theRef;
  198. {
  199.     int io;
  200.     
  201.     io=Create(fn, *vRef, 'CEM8', 'TEXT');
  202.     if ((io==noErr) || (io==dupFNErr)) io = FSOpen( fn, *vRef, theRef );
  203.     return( (io==noErr) || (io=dupFNErr) );
  204. }
  205.  
  206. WriteFile( refNum, p, num )
  207. int        refNum;
  208. char    *p;
  209. long    num;
  210. {
  211.     int io;            /*     a real application would want to check 
  212.                         this return code for failures */
  213.     
  214.     io=FSWrite( refNum, &num, p );
  215. }
  216.  
  217. ReadFile( refNum, textH )
  218. int        refNum;
  219. TEHandle textH;
  220. {
  221.     char    buffer[256];
  222.     long    count;
  223.     int        io;
  224.     
  225.     TESetSelect(0, (**textH).teLength, textH);
  226.     TEDelete( textH );
  227.     do {
  228.         count = 256;
  229.         io = FSRead( refNum, &count, &buffer );
  230.         TEInsert( &buffer, count, textH );
  231.     } while (io==noErr);
  232.     
  233.     return( io==eofErr );
  234. }
  235.  
  236. pStrCopy( p1, p2 )
  237. register char *p1, *p2;
  238. /* copies a pascal string from p1 to p2 */
  239. {
  240.     register int len;
  241.     
  242.     len = *p2++ = *p1++;
  243.     while (--len>=0) *p2++=*p1++;
  244. }
  245.  
  246. FileError( s, f )
  247. Str255 s, f;
  248. {
  249.     ParamText(s, f,"\p", "\p");
  250.     Alert( ErrorAlert, 0L );
  251. }
  252.